﻿//**********************************************************
//Copyright(C)2011 By 波特商网公司 版权所有。
//
//文件名：UpdateHelper.cs
//文件功能：
//
//创建标识：齐李平 || 2011-07-15 16:26:00
//
//修改标识：
//修改描述：
//**********************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newborn.CSCommon;
using System.Xml.Linq;
using System.Diagnostics;
using System.Windows.Forms;


namespace Newborn.Store.Core
{
    /// <summary>
    /// UpdateHelper
    /// </summary>
    public class UpdateHelper
    {
        private static string currentServerVersion = "";//当前版本
        private static string localVersion = "";//本地版本
        //主程序名
        private static string mainAppExe = System.IO.Path.GetFileName(Application.ExecutablePath);
        //服务器XML
        private static XDocument serverXDoc = null;

        /// <summary>
        /// 检查是否需要更新
        /// </summary>
        /// <returns></returns>
        public static bool CheckUpdate()
        {
            #region 本地版本信息
            try
            {
                //当前主程序版本
                localVersion = FileVersionInfo.GetVersionInfo(System.IO.Directory.GetCurrentDirectory() + "\\" + mainAppExe).FileVersion;
            }
            catch
            {

            }

            #endregion

            #region 服务器版本信息
            try
            {
                StoreContractsClient updateServer = new StoreContractsClient();
                //字符串转换成XDocument
                serverXDoc = XDocument.Parse(updateServer.CheckStoreUpdateXML(localVersion));
            }
            catch (EntryPointNotFoundException ex)
            {
                //Recorder.Write(ex);
                return false;//EnumUpgradeState.ServerError;
            }
            catch
            {
                return false;//EnumUpgradeState.ServerError;
            }

            try
            {
                currentServerVersion = serverXDoc.Element("AutoUpdater").Element("Application").Element("Version").Value;
                //mainAppExe = XmlHelper.GetMainApplication(serverXDoc);
            }
            catch (Exception ex)
            {
                //Recorder.Write(ex);
                return false;// EnumUpgradeState.ConfigFileError;

            }

            #endregion

            #region 判断是否需要更新
            if (currentServerVersion != localVersion)
            {
                //需要更新
                return true;
            }
            else
            {
                return false;
            }

            #endregion
        }
    }

    /// <summary>
    /// 更新状态枚举
    /// </summary>
    public enum EnumUpgradeState
    {
        /// <summary>
        /// 服务器错误
        /// </summary>
        //[EnumAttribute("服务器错误")]
        ServerError,
        /// <summary>
        /// 配置文件错误
        /// </summary>
        //[EnumAttribute("配置文件错误")]
        ConfigFileError,
        /// <summary>
        /// 不需要更新
        /// </summary>
        //[EnumAttribute("不需要更新")]
        NoNeedToUpdate,
        /// <summary>
        /// 版本更新
        /// </summary>
        //[EnumAttribute("版本更新")]
        VersionUpdate,
        /// <summary>
        /// 重新安装
        /// </summary>
        //[EnumAttribute("重新安装")]
        Reinstall,
        /// <summary>
        /// 更新错误
        /// </summary>
        //[EnumAttribute("更新错误")]
        UpdateSuccess
    }
}
